<?php
namespace Dms\Controller;
use Dms\Controller\CommonController;

class MenuController extends CommonController
{   
    /**
     * [index description]
     * @return [type] [description]
     */
    public function index()
    {
    	$list = M('menu')->where(array('pid'=>0))->order(array('sort'=>'ASC','status'=>'DESC'))->select();
    	foreach ($list as $key => $value)
    	{  
    		$list[$key]['parent'] = M('menu')->where(array('pid'=>$value['id']))->order(array('sort'=>'ASC','status'=>'DESC'))->select();
            foreach ($list[$key]['parent'] as $k => $v) 
            {
                $list[$key]['parent'][$k]['child'] = M('menu')->where(array('pid'=>$v['id']))->order(array('sort'=>'ASC','status'=>'DESC'))->select();
            }
    	}
    	$this->list = $list;

        $this->menuID   = '48';
        $this->menuName = '菜单列表';
    	$this->display();
    }
    /**
     * [add description]
     */
    public function add()
    {
    	$pid = I('get.pid',0);
        $this->pid = $pid;

    	if(IS_POST)
    	{
    		$data['pid'] 		= $pid;
    		$data['name'] 		= I('post.name');
    		$data['url']  		= I('post.url');
    		$data['sort'] 		= I('post.sort');
    		$data['status']  	= I('post.status');

    		$insertID = M('menu')->add($data);

    		if($insertID != false)
    		{
    			$this->success('添加成功',U(MODULE_NAME.'/Menu/index'));
    		}
    		else
    		{
    			$this->error('添加失败');
    		}

    		exit;
    	}

    	$this->parentName = M('menu')->where(array('id' => $pid))->getField('name');

        $this->menuID   = '48';
        $this->menuName = '添加子菜单';
    	$this->display();
    }
    /**
     * [edit description]
     * @return [type] [description]
     */
    public function edit()
    {
    	$id = I('get.id',0);
        $this->id = $id;

    	if(IS_POST)
    	{
	    	$data['name'] 		= I('post.name');
    		$data['url']  		= I('post.url');
    		$data['sort'] 		= I('post.sort');
    		$data['status']  	= I('post.status');

	    	$updateID = M('menu')->where(array('id'=>$id))->save($data);
	    	if($updateID == true)
	    	{
	    		$this->success('修改成功',U(MODULE_NAME.'/Menu/index'));
	    	}
	    	else
	    	{
	    		$this->error('修改失败');
	    	}
    		exit;
    	}
        if($id)
        {
        	$list = M('menu')->where(array('id' => $id))->find();
        	$this->list = $list;
        }
        else
        {
            $this->redirect(MODULE_NAME.'/Menu/index');
        }

        $this->menuID   = '48';
        $this->menuName = '编辑菜单';
    	$this->display();
    }
    /**
     * [delete description]
     * @return [type] [description]
     */
    public function delete()
    {
    	$id = I('get.id',0);
        if($id)
        {
    	   $result = M('menu')->delete($id);
           if($result != false)
           {
                $this->success('删除成功',U(MODULE_NAME.'/Menu/index'));
           }
           else
           {
                $this->error('删除失败');
           }
           exit;
        }
    }
}